package com.webinsky.web.resource;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;

import com.webinsky.publish.domain.EmailSettingInfo;
import com.webinsky.publish.service.EmailSettingManagement;
import com.webinsky.web.data.EmailSettingForm;
import com.webinsky.web.data.Response;

import reactor.core.publisher.Mono;

@Component
public class EmailSettingResource {
	@Autowired
	private EmailSettingManagement emailSettingMangement;

	/**
	 * 生成更新邮件设置
	 * @param request
	 * @return
	 */
	public Mono<ServerResponse> createOrUpdateSetting(ServerRequest request){
		return request.bodyToMono(EmailSettingForm.class).flatMap( data -> {
			if ( data.siteId < 0 ) {
				return ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR).syncBody(Response.create("", Response.STATUS_INTERNAL_ERROR, "参数不对"));
			}
			EmailSettingInfo info = new EmailSettingInfo();
			info.setId(data.id);
			info.setPort(data.port);
			String[] strRecs = data.recievers.split(",");
			List<String> recievers = new ArrayList<>();
			for ( String s : strRecs ) {
				recievers.add(s);
			}
			info.setRecievers(recievers);
			info.setSender(data.sender);
			info.setSenderPasword(data.password);
			info.setSiteId(data.siteId);
			info.setSmtp(data.stmp);
			emailSettingMangement.createOrUpdate(info);
			return ServerResponse.ok().syncBody(Response.create(info.getId(), Response.STATUS_OK, ""));
		}).onErrorResume(t -> {
			return ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR).syncBody(Response.create("", Response.STATUS_INTERNAL_ERROR, t.getMessage()));
		});
	}
	

	/**
	 * 取得网站下的邮件设置
	 * 
	 * @param request
	 * @return
	 */
	public Mono<ServerResponse> getSettings(ServerRequest request){
		return Mono.just(request.queryParam("siteid").orElseGet(() -> "0")).flatMap( data -> {
			String siteId = data;
			EmailSettingInfo info = emailSettingMangement.loadBySiteId(Integer.valueOf(siteId));
			return ServerResponse.ok().syncBody(Response.create(info, Response.STATUS_OK, ""));
		}).onErrorResume( t -> {
			return ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR).syncBody(Response.create("", Response.STATUS_INTERNAL_ERROR, t.getMessage()));
		});
	}
	
	/**
	 * @param request
	 * @return
	 */
	public Mono<ServerResponse> testSendEmail(ServerRequest request){
		return Mono.just(request.queryParam("siteid").orElseGet(() -> "0")).flatMap( data -> {
			String siteId = data;
			boolean isOK = emailSettingMangement.testSendEmail(Integer.valueOf(siteId));
			return ServerResponse.ok().syncBody(Response.create(isOK, Response.STATUS_OK, ""));
		}).onErrorResume( t -> {
			return ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR).syncBody(Response.create("", Response.STATUS_INTERNAL_ERROR, t.getMessage()));
		});
	}
}
